<?php
/* --------------------------------------------------------------
   get_robots.php 2011-03-04 gambio
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2011 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/

/*
 * creates a robot file and exports it
 *
 * @param string $p_shop_path Shop path
 * @return string
 */
function get_robots($p_shop_path)
{
    $t_file = DIR_FS_CATALOG.'export/robots.txt.tpl';

    $t_lines = file($t_file);

    $t_result = '';
    foreach($t_lines as $line) {
        $t_result .= str_replace('{PATH}', $p_shop_path, $line);
    }

    // check SSL
    if(ENABLE_SSL_CATALOG == 'true') {
        // check if ssl is in a subdirectory
        $t_http_parsed = parse_url(HTTPS_CATALOG_SERVER);
        if(isset($t_http_parsed['path'])) {
            $t_result .= "\n\n";
            $t_path = substr($t_http_parsed['path'], 1);
            if(substr($t_path, -1, 1) != '/') {
                $t_path = $t_path.'/';
            }
            // again for ssl
            foreach($t_lines as $line) {
                $t_result .= str_replace('{PATH}', $p_shop_path.$t_path, $line);
            }
        }
    }

	// convert into UNIX-file format
	$t_result = str_replace("\r\n", "\n", $t_result);
	// convert into Windows-file format
	$t_result = str_replace("\n", "\r\n", $t_result);

    header("Expires: Mon, 26 Nov 1962 00:00:00 GMT");
    header("Last-Modified: ".gmdate("D,d M Y H:i:s")." GMT");
    header("Cache-Control: no-cache, must-revalidate");
    header("Pragma: no-cache");
    header("Content-Type: Application/octet-stream");
    header("Content-disposition: attachment; filename=\"robots.txt\"");

    echo $t_result;
    exit;
}
?>